﻿using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnesSystem.Controls
{
    /// <summary>
    /// VerifierTextBoxCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class VerifierTextBoxCtrl : UserControl
    {

        #region Variables

        private string text = "";
        private int _verifierModel = 0;
        private IntPtr hwd = IntPtr.Zero;

        #endregion

        #region Constructor
        public VerifierTextBoxCtrl()
        {
            InitializeComponent();
            VerifierModel = 0;
            TextWrapping = TextWrapping.Wrap;
            IsEdit = true;
            this.Loaded += VerifierTextBoxCtrl_Loaded;
        }


        #endregion

        #region Properties

        public new Style Style
        {
            get
            {
                return _txt.Style;
            }
            set
            {
                _txt.Style = value;
            }
        }

        public bool IsEdit
        {
            set
            {
                if (value)
                    _tb.Visibility = Visibility.Collapsed;
                else
                    _tb.Visibility = Visibility.Visible;
            }
        }

        public InputMethodState InputMethodState
        {
            get
            {
                return InputMethod.Current.ImeState;
            }
            set
            {
                InputMethod.SetPreferredImeState(_txt, value);
            }
        }

        /// <summary>
        /// （默认不验证）0：不验证 1：纯数字验证 2：double验证
        /// </summary>
        public int VerifierModel
        {
            get
            {
                return _verifierModel;
            }
            set
            {
                _verifierModel = value;
                //if (VerifierModel == 1 || VerifierModel == 2)
                //{
                //    InputMethod.SetPreferredImeState(_txt, InputMethodState.Off);
                //}
                //else
                //{
                //    InputMethod.SetPreferredImeState(_txt, InputMethodState.On);
                //}
            }
        }

        public int SelectionStart
        {
            get
            {
                return _txt.SelectionStart;
            }
            set
            {
                _txt.SelectionStart = value;
            }
        }

        public TextWrapping TextWrapping
        {
            get
            {
                return _txt.TextWrapping;
            }
            set
            {
                _txt.TextWrapping = value;
            }
        }

        #endregion

        #region 依赖属性

        public object Text
        {
            get { return (object)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        private static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(object), typeof(VerifierTextBoxCtrl), new PropertyMetadata("", TextChangedCallBack));

        private static void TextChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {

            }
        }



        public TextAlignment TextAlignment
        {
            get { return (TextAlignment)GetValue(TextAlignmentProperty); }
            set { SetValue(TextAlignmentProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TextAlignment.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextAlignmentProperty =
            DependencyProperty.Register("TextAlignment", typeof(TextAlignment), typeof(VerifierTextBoxCtrl), new PropertyMetadata(TextAlignment.Center));



        #endregion

        #region Event

        public event EventHandler<TextChangedEventArgs> TextChanged;

        public new event EventHandler<RoutedEventArgs> GotFocus;

        public new event EventHandler<RoutedEventArgs> LostFocus;

        public new event EventHandler<MouseButtonEventArgs> MouseLeftButtonDown;

        #endregion

        #region Private Methods

        /// <summary>
        /// 验证结果
        /// </summary>
        /// <returns></returns>
        private bool Verifier(string input, int startIndex)
        {
            bool result = false;
            string str = input.ToDBC();
            if (VerifierModel == 0)
            {
                result = true;
            }
            else if (VerifierModel == 1)
            {
                int intOut;
                if (int.TryParse(str, out intOut))
                {
                    result = true;
                }
                else
                {
                    result = false;
                }
            }
            else if (VerifierModel == 2)
            {
                double doubleOut;
                int intOut;
                if (str.Equals("."))
                {
                    if (_txt.Text.Length == 0 || (_txt.Text.Length != 0 && _txt.Text.Contains('.')) || startIndex == 0)
                    {
                        result = false;
                    }
                    else
                    {
                        result = true;
                    }
                }
                else
                {
                    result = double.TryParse(str, out doubleOut) || int.TryParse(str, out intOut);
                }
            }

            return result;
        }

        #endregion

        #region Public/Protected Methods

        #endregion

        #region EventHandlers

        private void VerifierTextBoxCtrl_Loaded(object sender, RoutedEventArgs e)
        {
            _tb.Width = ActualWidth;
            _tb.Height = ActualHeight;
        }


        private void _txt_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !Verifier(e.Text, _txt.SelectionStart);
        }

        private void _txt_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextChanged?.Invoke(sender, e);
        }
        private void _txt_GotFocus(object sender, RoutedEventArgs e)
        {
            _txt.SelectAll();
            _txt.PreviewMouseLeftButtonDown -= _txt_PreviewMouseLeftButtonDown;
            GotFocus?.Invoke(this, e);
        }

        private void _tb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            GotFocus?.Invoke(this, e);
        }

        private void _txt_LostFocus(object sender, RoutedEventArgs e)
        {
            LostFocus?.Invoke(this, e);
            _txt.PreviewMouseLeftButtonDown += _txt_PreviewMouseLeftButtonDown;
        }

        private void _txt_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _txt.Focus();
            e.Handled = true;
        }

        #endregion

       
    }
}
